package com.gimi.cloud.jimi.manage.consumer.service.prod.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.EntityVerification;
import com.gimi.cloud.jimi.common.utils.JsonUtil;
import com.gimi.cloud.jimi.common.utils.StringUtil;
import com.gimi.cloud.jimi.common.utils.reqhead.Response;
import com.gimi.cloud.jimi.common.utils.reqhead.Util;
import com.gimi.cloud.jimi.dto.prod.ProdCatModelDTO;
import com.gimi.cloud.jimi.dto.prod.ProdCategoryDTO;
import com.gimi.cloud.jimi.manage.consumer.feign.ProdCategoryFeignService;
import com.gimi.cloud.jimi.manage.consumer.service.prod.CategoryService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Service
public class CategoryServiceImpl implements CategoryService {
    private static final Logger logger = LoggerFactory.getLogger(CategoryServiceImpl.class);
    private final ProdCategoryFeignService prodCategoryFeignService;

    @Autowired
    public CategoryServiceImpl(ProdCategoryFeignService prodCategoryFeignService){
        this.prodCategoryFeignService = prodCategoryFeignService;
    }

    @Override
    public Response getCategoryList(@RequestBody String requestBody, HttpServletRequest request, HttpServletResponse response) {
        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,ProdCategoryDTO.class);
        return prodCategoryFeignService.getCategoryList(prodCategoryDTO);
    }

    @Override
    public Response addCategory(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,ProdCategoryDTO.class);
        if(StringUtil.isBlank(prodCategoryDTO.getParentCatId())||StringUtil.isBlank(prodCategoryDTO.getProdCatName())||
                StringUtil.isBlank(prodCategoryDTO.getCatGrade())||StringUtil.isBlank(prodCategoryDTO.getImgUrl())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        checkParentLength(prodCategoryDTO);

        //如果是添加三级品类，需要添加品牌
        if(3==prodCategoryDTO.getCatGrade()){
           if(StringUtil.isBlank(prodCategoryDTO.getPdId())){
               throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"添加三级品类失败，需要添加品牌，请输入品牌id！");
           }
        }

        return prodCategoryFeignService.saveOrUpdate(prodCategoryDTO);
    }

    @Override
    public Response updateCategory(String requestBody, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,ProdCategoryDTO.class);
        if(StringUtil.isBlank(prodCategoryDTO.getProdCatId())||StringUtil.isBlank(prodCategoryDTO.getParentCatId())||StringUtil.isBlank(prodCategoryDTO.getProdCatName())||
                StringUtil.isBlank(prodCategoryDTO.getCatGrade())||StringUtil.isBlank(prodCategoryDTO.getImgUrl())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        checkParentLength(prodCategoryDTO);
        //如果是添加三级品类，需要添加品牌
        if(3==prodCategoryDTO.getCatGrade()){
            if(StringUtil.isBlank(prodCategoryDTO.getPdId())){
                throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR,"修改三级品类失败，需要添加品牌，请输入品牌id！");
            }
        }
        return prodCategoryFeignService.saveOrUpdate(prodCategoryDTO);
    }

    /**
     * 添加和修改的时候检查父级id的长度
     */
    private void checkParentLength(ProdCategoryDTO prodCategoryDTO){
        int parentLength = prodCategoryDTO.getParentCatId().length();
        if(prodCategoryDTO.getCatGrade()==1 ){
            if( parentLength !=1){
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }else if (prodCategoryDTO.getCatGrade()==2 ){
            if( parentLength !=2) {
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }else {
            if( parentLength !=5) {
                throw new MsgException(ResponseCodeEnum.ADD_PRODCATEGORY_IS_NOT_SURE,"父品类Id错误");
            }
        }
    }

    @Override
    public Response deleteCategory(String requestBody, HttpServletRequest request, HttpServletResponse response) {
        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,ProdCategoryDTO.class);
        if(prodCategoryDTO==null || StringUtil.isBlank(prodCategoryDTO.getProdCatId()) || StringUtil.isBlank(prodCategoryDTO.getIsDelete())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodCategoryFeignService.categoryDelete(prodCategoryDTO);
    }

    @Override
    public Response getCategoryPageList(String requestBody, HttpServletRequest request, HttpServletResponse response)  throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,ProdCategoryDTO.class);
        if(StringUtil.isBlank(prodCategoryDTO.getPageSize())||StringUtil.isBlank(prodCategoryDTO.getCurrentPage())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodCategoryFeignService.getCategoryPageList(prodCategoryDTO);
    }

//    @Override
//    public void gotoEditProdCategory(String requestBody, HttpServletRequest request, HttpServletResponse response)  throws MsgException {
//        logger.info("请求参数===>" + JsonUtil.replaceNow(requestBody));
//        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestBody,ProdCategoryDTO.class);
//        if(StringUtil.isBlank(prodCategoryDTO.getProdCatId())){
//            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
//        }
//        Util.sendResponse(prodCategoryFeignService.gotoEditProdCategory(prodCategoryDTO),response);
//    }

    @Override
    public Response loadPordCategoryAndInitModelInfo(String requestData, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestData));
        ProdCategoryDTO ecProdCategoryDTO = JsonUtil.jsonToBean(requestData,ProdCategoryDTO.class);
        if(StringUtil.isBlank(ecProdCategoryDTO.getProdCatId())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodCategoryFeignService.loadPordCategoryAndInitModelInfo(ecProdCategoryDTO);
    }

    @Override
    public Response getThreeProdCatModel(String requestData, HttpServletRequest request, HttpServletResponse response) throws MsgException {
        logger.info("请求参数===>" + JsonUtil.replaceNow(requestData));
        ProdCatModelDTO prodCatModelDTO = JsonUtil.jsonToBean(requestData,ProdCatModelDTO.class);
        EntityVerification.checkByGroupsThrowsException(prodCatModelDTO, ProdCatModelDTO.ThreeProdCatModel.class);
        return prodCategoryFeignService.getThreeProdCatModel(prodCatModelDTO);
    }

    @Override
    public Response searchCategoryListByGrade(String requestData, HttpServletRequest request, HttpServletResponse response) {
        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestData,ProdCategoryDTO.class);
        if(StringUtil.isBlank(prodCategoryDTO.getCatGrade())){
            throw new MsgException(ResponseCodeEnum.PARAMETER_ERROR);
        }
        return prodCategoryFeignService.searchCategoryListByGrade(prodCategoryDTO);
    }

	@Override
	public Response fileUpload(HttpServletResponse response, HttpServletRequest request) throws Exception {
		StandardMultipartHttpServletRequest multipartHttpServletRequest = (StandardMultipartHttpServletRequest) request;
        MultiValueMap multipartFiles = multipartHttpServletRequest.getMultiFileMap();
        return prodCategoryFeignService.fileUpload(multipartFiles);
	}

    @Override
    public Response getCategoryListTree(String requestData, HttpServletRequest request, HttpServletResponse response) {
        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestData,ProdCategoryDTO.class);
        return prodCategoryFeignService.getCategoryListTree(prodCategoryDTO);
    }

    @Override
    public Response getCategoryListByParent(String requestData, HttpServletRequest request, HttpServletResponse response) {
        ProdCategoryDTO prodCategoryDTO = JsonUtil.jsonToBean(requestData,ProdCategoryDTO.class);
        if(prodCategoryDTO == null || StringUtil.isBlank(prodCategoryDTO.getProdCatId())){
            throw new MsgException(ResponseCodeEnum.INVALID,"品类id不能为空");
        }
        return prodCategoryFeignService.getCategoryListByParent(prodCategoryDTO);
    }
}
